<template>
  <div class="profile-box">
    <!-- 未登录 -->

    <div class="not-login header-bg" v-if="!user">
      <img src="@/assets/mobile.png" alt="" @click="$router.push('/login')">
      <span>登录/注册</span>
    </div>
    <!-- 已登录 -->
    <div class="userInfo header" v-else>
      <!-- 基本信息 -->
      <div class="base">
        <div class="left">
          <van-image round width="1rem" height="1rem" src="https://img01.yzcdn.cn/vant/cat.jpeg" />
          <span class="name">黑马小姐姐</span>
        </div>
        <div class="right">
          <van-button type="default" size="mini" round>编辑资料</van-button>
        </div>
      </div>
      <!-- 粉丝、关注 -->
      <div class="user-data">
        <div class="data-item">
          <span>{{userInfo.art_count}}</span>
          <span>关注</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.follow_count}}</span>
          <span>头条</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.fans_count}}</span>
          <span>粉丝</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.like_count}}</span>
          <span>点赞</span>
        </div>
      </div>
    </div>
    <!-- 宫格 -->
    <van-grid class="nav-grid" :column-num="2" clickable>
      <van-grid-item text="收藏">
        <i slot="icon" class="toutiao toutiao-shoucang"></i>
        <span class="text" slot="text">收藏</span>
      </van-grid-item>
      <van-grid-item text="文字">
        <i slot="icon" class="toutiao toutiao-lishi"></i>
        <span class="text" slot="text">历史</span>
      </van-grid-item>
    </van-grid>
    <div class="box-cell">
      <van-cell-group>
        <van-cell title="消息通知" />
        <van-cell title="小智同学" />
      </van-cell-group>
      <van-cell class="logout-call" title="退出登录" v-if="user" @click="logout"> </van-cell>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserProfile } from '@/app/user.js'
export default {
  name: 'profile',
  data () {
    return {
      userInfo: {}
    }
  },
  methods: {
    // 点击退出
    logout () {
      this.$dialog.confirm({
        title: '黑马头条',
        message: '确定退出'
      })
        .then(() => {
          // on confirm
          // 存数据   // 确认退出：清除登录状态（容器中的 user + 本地存储中的 user）
          this.$store.commit('initUser', null)
        })
        .catch(() => {
          // on cancel
          console.log('失败')
        })
    },
    // 加载用户信息
    async loadUser () {
      try {
        const res = await getUserProfile()
        console.log(res)
        this.userInfo = res.data.data
      } catch (err) {
        this.$toast('获取失败')
      }
    }

  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  filters: {},
  components: {},
  created () {
    // 初始化的时候调用数据从vuex拿数据
    if (this.user) {
      this.loadUser()
    }
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
.profile-box {
  .header-bg {
    width: 750px;
    height: 400px;
    background-image: url(~@/assets/banner.png);
    background-size: 100%;
  }
  // 未登录
  .not-login {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    img {
      width: 132px;
      height: 130px;
    }
    span {
      font-size: 28px;
      color: #fff;
      margin-top: 15px;
    }
  }
  .header {
    width: 750px;
    height: 401px;
    background: url('~@/assets/banner.png');
    background-size: cover;
  }
  //登录
  .userInfo {
    // background: red;
    background: #49a2fb;
    .base {
      height: 231px;
      padding: 76px 32px 23px;
      background: #ccc;
      display: flex;
      justify-content: space-between;
      align-items: center;
      box-sizing: border-box;
      .left {
        display: flex;
        align-items: center;
        .avatar {
          width: 132px;
          height: 132px;
          border: 2px solid #fff;
        }
        .name {
          font-size: 29px;
          margin-left: 22px;
        }
      }
    }
    .user-data {
      height: 153px;
      display: flex;
      .data-item {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;

        color: #fff;
        span:nth-child(1) {
          font-size: 30px;
          margin-bottom: 20px;
        }
        span:nth-child(2) {
          font-size: 22px;
        }
      }
    }
  }
  .nav-grid {
    .toutiao {
      font-size: 45px;
    }
    .toutiao-shoucang {
      color: #eb5253;
    }
    .toutiao-lishi {
      color: #ff9d1d;
    }
    .text {
      font-size: 27px;
      color: #333;
      margin-top: 16px;
      letter-spacing: 10px; //字间距
    }
  }
  .box-cell {
    margin: 10px 0;
  }
  .logout-call {
    text-align: center;
    color: #d86262;
  }
}
</style>
